<?php

namespace App\Admin\Controllers\goods\actions;


use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;

class OrderPass extends AbstractTool
{
    protected function script()
    {
        $warning = '请先选择勾选要操作的记录';

        $url = route('admin.orderPass');

        return <<<EOT
        
$('.audit-action').click(function () {
    var hint = $(this).data('type');
    var message = '';
    if (hint == '5') {
        message = '通过选中的记录'
    } else {
        message = '驳回选中的记录'        
    }
    
    swal({
        title: "确认" + message + "?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        showLoaderOnConfirm: true,
        cancelButtonText: "取消",
        preConfirm: function() {
            
            var key = $.admin.grid.selected();
            var data = {};        
            if (key.length === 0) {
                $.admin.toastr.warning('{$warning}', '', {positionClass: 'toast-top-center'});
                return ;
            }
            
            Object.assign(data, {ids: key});
        
            console.log(data);
            
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '{$url}',
                    data: {
                        _token:LA.token,
                        status: hint,
                        bag: data
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});

EOT;
    }

    protected function html()
    {
        return <<<'HTML'
    <button class="btn btn-sm btn-info audit-action" data-type="5"><i class='fa fa-check-circle'></i>退款通过</button>
    <button class="btn btn-sm btn-danger audit-action" data-type="3"><i class='fa fa-minus-circle'></i>退款驳回</button>
HTML;
    }

    public function render()
    {
        Admin::script($this->script());

        return $this->html();
    }

}